package com.zmn.oms.dubbo.impl.zmn.master.order;

import com.alibaba.fastjson.JSON;
import com.zmn.consts.GlobalConsts;;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.attachment.AttachmentBService;
import com.zmn.oms.common.dio.attachment.ModifyOrderWorkAttachmentDIO;
import com.zmn.oms.common.dio.attachment.OrderWorkAttachmentDIO;
import com.zmn.oms.common.dro.attachment.WorkAttachmentDRO;
import com.zmn.oms.dubbo.interfaces.normal.master.MasterOrderAttachmentListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.orderattachment.ModifyOrderAttachmentDTO;
import com.zmn.oms.model.entity.orderattachment.OrderAttachmentQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;

/**
 * 类描述：师傅工单附件
 *
 * @author liudi
 * @since: 2022/3/29 14:44
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class MasterOrderAttachmentListRemoteServiceImpl implements MasterOrderAttachmentListRemoteService {

	@Autowired
	private AttachmentBService attachmentBService;

	@Override
	public ResponseDTO<WorkAttachmentDRO> listOrderWorkAttachmentByQuery(@NotNull(message = "入参不能为空") OrderWorkAttachmentDIO orderWorkAttachmentDIO) {
		try {
			log.debug("根据渠道配置查询【师傅工单照片附件】 入参：{}", orderWorkAttachmentDIO);
			OrderAttachmentQuery orderAttachmentQuery = BeanMapper.map(orderWorkAttachmentDIO, OrderAttachmentQuery.class);
			WorkAttachmentDRO workAttachmentDRO = attachmentBService.getOrderAttachmentAndConfigByQuery(orderAttachmentQuery);
			log.debug("根据渠道配置查询【师傅工单照片附件】 出参：{}", workAttachmentDRO);
			return ResponseDTO.success(workAttachmentDRO);
		} catch (Exception exception) {
			log.error("{}根据渠道配置查询【师傅工单照片附件】！异常：{}", JSON.toJSON(orderWorkAttachmentDIO), exception);
			return ResponseDTO.fail(String.format("查询工单照片附件失败，请稍后重试。异常：%s", exception.getMessage()));
		}
	}

	@Override
	public ResponseDTO modifyOrderWorkAttachment(ModifyOrderWorkAttachmentDIO modifyOrderWorkAttachmentDIO) {
		try {
			if (log.isDebugEnabled()) {
				log.debug("保存工单附件 Dubbo 接口，入参：{}", JSON.toJSON(modifyOrderWorkAttachmentDIO));
			}
			ModifyOrderAttachmentDTO modifyOrderAttachmentDTO = BeanMapper.map(modifyOrderWorkAttachmentDIO, ModifyOrderAttachmentDTO.class);
			modifyOrderAttachmentDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
			modifyOrderAttachmentDTO.setOperatorId((long)modifyOrderWorkAttachmentDIO.getMasterId());
			attachmentBService.modifyOrderWorkAttachment(modifyOrderAttachmentDTO);
			return ResponseDTO.success();
		} catch (Exception ex) {
			log.error("{}保存工单附件失败！异常：{}", JSON.toJSON(modifyOrderWorkAttachmentDIO), ex);
			return ResponseDTO.fail("保存工单附件失败，请稍后重试。");
		}
	}


}
